﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Quartz;
using Quartz.Impl;

namespace NetCoreQuartzDemo
{
    class Program
    {
        private static IScheduler scheduler;
        static void Main(string[] args)
        {


            //Jobs();
            // Task.Run(() => { Jobs(); });

            //var baseType = typeof(Ibase);

            //baseType.Assembly.GetTypes().Where(x => typeof(Ibase).IsAssignableFrom(x)).Where(t => !t.IsAbstract && t.IsClass) //获取非抽象类 排除接口继承
            //                                                                                                               .ToList().ForEach(c =>
            //{
            //    Console.WriteLine(c.FullName);
            //    Console.WriteLine(c.Name);
            //});

            //var dic = new Dictionary<string, int> { { "MyJob", 10 }, { "OneJob", 20 }, { "TwoJob", 30 }, { "ThreeJob", 40 }, { "FourJob", 50 } };
            //手动指定计划任务
            //Task.Run(() => { MultJobs(dic); });
            //自动注册计划任务
            Task.Run(() => { MultJob(); });
            Console.Read();
        }

        static async Task<Task> Jobs()
        {
            //1、声明一个调度工厂
            var schedulerFactory = new StdSchedulerFactory();
            //2、通过调度工厂获得调度器
            //var scheduler = await schedulerFactory.GetScheduler();
            scheduler = await schedulerFactory.GetScheduler();
            //3、开启调度器
            await scheduler.Start();
            //4、创建一个触发器
            var trigger = TriggerBuilder.Create().WithSimpleSchedule(x =>
                {
                    x.WithIntervalInSeconds(1).RepeatForever();
                }).Build();
            //5、创建任务
            var jobDetail = JobBuilder.Create<MyJob>()
                .WithIdentity("job", "group")
                .Build();
            //6、将触发器和任务器绑定到调度器中
            await scheduler.ScheduleJob(jobDetail, trigger);
            await scheduler.PauseJob(new JobKey("job"));
            Console.WriteLine("暂停10秒");
            Thread.Sleep(new TimeSpan(0, 0, 0, 10));
            Console.WriteLine("恢复");
            await scheduler.ResumeJob(new JobKey("job"));
            return Task.Run(() => { Console.WriteLine("完成"); });
        }

        static async Task<Task> MultJobs(Dictionary<string, int> dic)
        {
            //1、声明一个调度工厂
            var schedulerFactory = new StdSchedulerFactory();
            //2、通过调度工厂获得调度器
            //var scheduler = await schedulerFactory.GetScheduler();
            scheduler = await schedulerFactory.GetScheduler();
          

            var baseType = typeof(Ibase);
            baseType.Assembly.GetTypes().Where(x => typeof(Ibase).IsAssignableFrom(x)).Where(t => !t.IsAbstract && t.IsClass) //获取非抽象类 排除接口继承
                .ToList().ForEach(c =>
                {
                    if (dic.ContainsKey(c.Name))
                    {
                        //4、创建一个触发器
                        var trigger = TriggerBuilder.Create().WithSimpleSchedule(x =>
                        {
                            x.WithIntervalInSeconds(dic[c.Name]).RepeatForever();
                        }).StartAt(DateTime.Now.AddSeconds(dic[c.Name])).Build();
                        //创建任务
                        IJobDetail job = new JobDetailImpl(c.Name, "myjob", c);
                        //将触发器和任务器绑定到调度器中
                        scheduler.ScheduleJob(job, trigger);
                       
                    }
                });

            //3、开启调度器 await scheduler.Start(); 等同于 scheduler.Start();
            await scheduler.Start();

            return Task.Run(() => { Console.WriteLine("完成"); });
        }

        static async Task<Task> MultJob()
        {
            //1、声明一个调度工厂
            var schedulerFactory = new StdSchedulerFactory();
            //2、通过调度工厂获得调度器
            //var scheduler = await schedulerFactory.GetScheduler();
            scheduler = await schedulerFactory.GetScheduler();

            var baseType = typeof(Ibase);
            baseType.Assembly.GetTypes().Where(x => typeof(Ibase).IsAssignableFrom(x)).Where(t => !t.IsAbstract && t.IsClass) //获取非抽象类 排除接口继承
                .ToList().ForEach(c =>
                {
                    //检查继承Ibase的类是否 包含QuartzSet 特性
                    if (c.IsDefined(typeof(QuartzSetAttribute),true))
                    {
                        //获取特性内容
                        QuartzSetAttribute attribute = (QuartzSetAttribute)c.GetCustomAttribute(typeof(QuartzSetAttribute), true);
                        //4、创建一个触发器
                        var trigger = TriggerBuilder.Create().WithSimpleSchedule(x =>
                        {
                            //注册间隔时间 使用TimeSpan
                            x.WithInterval(new TimeSpan(attribute.Day, attribute.Hour, attribute.Minutes,attribute.Second)).RepeatForever();
                            //x.WithIntervalInSeconds(attribute.Second).RepeatForever();
                        }).StartAt(DateTime.Now.AddSeconds(attribute.Second)).Build();
                        //创建任务
                        IJobDetail job = new JobDetailImpl(c.Name, attribute.JobGroup, c);
                        //将触发器和任务器绑定到调度器中
                        scheduler.ScheduleJob(job, trigger);
                        Console.WriteLine($"已自动注册 计划任务【{attribute.JobName}】 类名：{c.FullName} 时间间隔:【{attribute.Day}】天 【{attribute.Hour}】时【{attribute.Minutes}】 分【{attribute.Second}】秒");
                    }
                });

            //3、开启调度器 await scheduler.Start(); 等同于 scheduler.Start();
            await scheduler.Start();



            return Task.Run(() => { Console.WriteLine("完成"); });
        }
    }
}
